package com.example.booksystem.service.impl;

import com.example.booksystem.mapper.UserMapper;
import com.example.booksystem.model.User;
import com.example.booksystem.service.UserService;

import java.sql.SQLException;
import java.util.List;

public class UserImpl implements UserService {
    private UserMapper userMapper = new UserMapper();

    @Override
    public User login(User user) {
        User u;
        try {
            u = userMapper.getUser(user.getUsername());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if (u == null){
            return null;//账户不存在
        }
        if (user.getPassword().equals(u.getPassword())){
            return u;//返回用户类型
        }else {
            user.setUserType(-1);
            return user;//密码错误
        }
    }

    @Override
    public Integer sign(User user) throws SQLException {
        return userMapper.insertUser(user);
    }

    @Override
    public List<User> users() throws SQLException {
        return userMapper.selectUser();
    }

    @Override
    public Boolean toAdmin(Integer id) throws SQLException {
        if (userMapper.UserToAdmin(id) == 1){
            return true;
        }
        return false;
    }

    @Override
    public Boolean toNomal(Integer id) throws SQLException {
        if (userMapper.UserToNomal(id) == 1){
            return true;
        }
        return false;
    }

    @Override
    public Boolean delete(Integer id) throws SQLException {
        if (userMapper.delete(id) == 1){
            return true;
        }
        return false;
    }

    @Override
    public Boolean detele(Integer id) throws SQLException {
        if (userMapper.delete(id) == 1){
            return true;
        }
        return false;

    }
}
